package com.ysd.productcate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ysd.common.entity.Productcate;
import com.ysd.productcate.service.impl.ProductCateServiceImpl;

@Controller
@RequestMapping("/productcate")
public class ProductCateController {
	@Autowired
	private ProductCateServiceImpl productcateservice;
	
	@Value("${server.port}")
	private String port;
	
	@Value("${spring.cloud.client.ip-address}")
	private String ip;
	
	
	/**
	 * 测试路径:http://localhost:9006/productcate/findCateAllBySelAndPage?p_id=1&page=1&limit=10
	 * 根据条件查询并分页
	 * @param p_id
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/findCateAllBySelAndPage")
	@ResponseBody
	public Map<String, Object> findCateAllBySelAndPage(int p_id,int page,int limit){
		page=(page-1)*10;
		List<Productcate> list=productcateservice.findCateAllBySelAndPage(p_id, page, limit);
		int total=productcateservice.findProductCateTotal(p_id, page, limit);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("data", list);
		map.put("code", 0);
		map.put("count", total);
		return map;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/saveProductCate?name=裤子&p_id=0
	 * 添加商品类型
	 * @param productcate
	 * @return
	 */
	@RequestMapping("/saveProductCate")
	@ResponseBody
	public Map<String, Object> saveProductCate(Productcate productcate){
		int r=productcateservice.saveProductCate(productcate);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("code", 0);
			map.put("success", true);
			map.put("message", "添加成功");
		}else {
			map.put("code", 0);
			map.put("success", false);
			map.put("message", "添加失败");
		}
		return map;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/updateProductCate?name=裤子66666&p_id=0&id=10
	 * 添加商品类型
	 * @param productcate
	 * @return
	 */
	@RequestMapping("/updateProductCate")
	@ResponseBody
	public Map<String, Object> updateProductCate(Productcate productcate){
		int r=productcateservice.updateProductCate(productcate);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("code", 0);
			map.put("success", true);
			map.put("message", "修改成功");
		}else {
			map.put("code", 0);
			map.put("success", false);
			map.put("message", "修改失败");
		}
		return map;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/delProductCate?id=10
	 * 添加商品类型
	 * @param productcate
	 * @return
	 */
	@RequestMapping("/delProductCate")
	@ResponseBody
	public Map<String, Object> delProductCate(int id){
		int r=productcateservice.delProductCate(id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("code", 0);
			map.put("success", true);
			map.put("message", "删除成功");
		}else {
			map.put("code", 0);
			map.put("success", false);
			map.put("message", "删除失败");
		}
		return map;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/findCateAllParent
	 * 查询所有父类类型
	 * @return
	 */
	@RequestMapping("/findCateAllParent")
	@ResponseBody
	public List<Productcate> findCateAllParent(){
		List<Productcate> list=productcateservice.findCateAllParent();
		return list;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/findCateAllChild?p_id=7
	 * 根据父类编号查询子类
	 * @return
	 */
	@RequestMapping("/findCateAllChild")
	@ResponseBody
	public List<Productcate> findCateAllChild(int p_id){
		List<Productcate> list=productcateservice.findCateAllChild(p_id);
		return list;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/findCateAllChilds
	 * 查询所有子分类
	 * @return
	 */
	@RequestMapping("/findCateAllChilds")
	@ResponseBody
	public List<Productcate> findCateAllChilds(){
		List<Productcate> list=productcateservice.findCateAllChilds();
		return list;
	}
	/**
	 * 测试路径:http://localhost:9006/productcate/findCateAll
	 * 前台商品类型二级分别列表
	 * @return list
	 */
	@RequestMapping("/findCateAll")
	@ResponseBody
	public Map<String, Object> findCateAll(){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Productcate> list=productcateservice.findCateAll();
		List<Productcate> onelist=new ArrayList<Productcate>();
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getP_id()==0) {
				List<Productcate> twolist=new ArrayList<Productcate>();
				for(int j=0;j<list.size();j++) {
					if(list.get(i).getId()==list.get(j).getP_id()) {
						twolist.add(list.get(j));
					}
				}
				list.get(i).setSecondMenus(twolist);
				onelist.add(list.get(i));
			}
		}
		for(int i=0;i<onelist.size();i++) {
			if(onelist.get(i).getSecondMenus().size()>0) {
				onelist.get(i).setActive(true);
			}
		}
		map.put("data", onelist);
		return map;
	}
}
